<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Contents;
use App\Models\ContentCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class ContentController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文档管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Contents(['category']), function ($grid) {
            if (request()->cat_id) {
                $grid->model()->where('category_id', request()->cat_id);
            }
            $grid->model()->whereNull('deleted_at');
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id');
            $grid->column('type')->display(function ($type) {
                return \App\Models\Content::$lib_type[$type];
            });
            $grid->column('category.name', __('所属分类'));
            $grid->column('lang')->display(function ($lang) {
                return \App\Models\Content::$lib_lang[$lang];
            });
            $grid->column('title');
            $grid->column('weight');
            $grid->column('status')->switch();
            $grid->column('is_home')->switch();
            $grid->column('created_at')->display(function ($created_at) {
                return $created_at;
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title')->width(3);
                $filter->equal('category_id')->select(ContentCategory::getSelectList())->width(3);
            });
        });


    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Contents(), function ($show) {
            $show->field('id');
            $show->type('类型')->as(function ($type) {
                return \App\Models\Content::$lib_type[$type];
            });
            $show->field('title');
            $show->field('alias');
            $show->lang('语言')->as(function ($lang) {
                return \App\Models\Content::$lib_lang[$lang];
            });
            $show->image('大图')->image('http://' . $_SERVER['HTTP_HOST'] . '/upload/');
            $show->field('url');
            $show->field('weight');
            $show->status('状态')->as(function ($status) {
                return tt_status($status);
            });
            $show->is_home('是否首页显示')->as(function ($is_home) {
                return tt_status($is_home);
            });
            $show->field('description', __('简述'));
            $show->field('body', __('正文'));
            $show->field('created_at');
            $show->field('updated_at', __('更新时间'));
            $show->field('deleted_at', __('删除时间'));
            $show->panel()->tools(function ($tools) {
                $tools->disableDelete();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Contents(), function (Form $form) {
            $form->text('title')->required();
            $form->text('alias');
            $form->select('type')->options(\App\Models\Content::$lib_type)->required();
            $form->select('lang')->options(\App\Models\Content::$lib_lang)->required();
            $form->select('category_id')->options('/content-categories/category-list');
            $form->image('image')->removable();
            $form->number('image_width');
            $form->number('image_height');
            $form->number('weight')->default(1);
            $states = [
                'on' => ['value' => STATUS_ENABLED, 'text' => '启用', 'color' => 'success'],
                'off' => ['value' => STATUS_DISABLED, 'text' => '禁用', 'color' => 'danger'],
            ];
            $form->switch('status')->options($states)->default(10);
            $form->switch('is_home')->options($states)->default(10);
            $form->url('url');
            $form->textarea('body')->required();
            $form->tools(function (Form\Tools $tools) {
                $tools->disableDelete();
            });

        });
    }
}
